package cake.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cake.bean.ShoppingCart;
import cake.db.MySqlDb;
//���ﳵ��ݿ���ʶ���
public class ShoppingCartDao {
private MySqlDb db=new MySqlDb();
	//ͨ��id��ȡ���ﳵ����
	public ShoppingCart getCart(long id)
	{
		ShoppingCart cart=null;
		String sql="select * from shoppingcart where id="+id;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				cart=new ShoppingCart();
				cart.setId(rs.getLong("id"));
				cart.setUserId(rs.getLong("userid"));
				cart.setCakeId(rs.getLong("cakeid"));
				cart.setQuantity(rs.getInt("quantity"));
				cart.setRemark(rs.getString("remark"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return cart;
	}
	//ͨ���û�id�Ͳ�Ʒid��ȡ���ﳵ����
	public ShoppingCart getCart(long userId,long cakeId)
	{
		ShoppingCart cart=null;
		String sql="select * from shoppingcart where userid="+userId+" and cakeid="+cakeId;
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				cart=new ShoppingCart();
				cart.setId(rs.getLong("id"));
				cart.setUserId(rs.getLong("userid"));
				cart.setCakeId(rs.getLong("cakeid"));
				cart.setQuantity(rs.getInt("quantity"));
				cart.setRemark(rs.getString("remark"));
				break;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally
		{
			db.Close();
		}
		
		return cart;
	}
	//��ҳ��ȡ�����б?��pageSizeΪ0ʱ��ʾ����ҳ
	public List<ShoppingCart> getCarts(int currPage,int pageSize,String where)
	{
		List<ShoppingCart> carts=new ArrayList<ShoppingCart>();
		String sql="select * from shoppingcart ";
		if(!where.equals(""))
		{
			sql+=" Where "+where;
		}
		if(pageSize!=0)
		{
			sql+=" limit "+((currPage-1)*pageSize)+","+pageSize;
		}
		
		try {
			ResultSet rs = db.query(sql);
			while(rs.next())
			{
				ShoppingCart cart=new ShoppingCart();
				cart.setId(rs.getLong("id"));
				cart.setUserId(rs.getLong("userid"));
				cart.setCakeId(rs.getLong("cakeid"));
				cart.setQuantity(rs.getInt("quantity"));
				cart.setRemark(rs.getString("remark"));
				
				carts.add(cart);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		
		return carts;
	}
	//�������ﳵ
	public boolean addCart(ShoppingCart cart)
	{
		String sql="Insert into shoppingcart(cakeid,quantity,userid,remark) values("+cart.getCakeId()+","+cart.getQuantity()+","+cart.getUserId()+",'"+cart.getRemark()+"')";
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
		
	}
	//�޸Ĺ��ﳵ
	public boolean updateCart(ShoppingCart cart)
	{
		ShoppingCart oldCart=getCart(cart.getId());
		
		if(oldCart==null)
		{
			return false;
		}
		
		String sql="Update ShoppingCart set quantity="+cart.getQuantity()+",remark='"+cart.getRemark()+"' where id="+cart.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	//ɾ���ﳵ
	public  boolean deleteCart(ShoppingCart cart)
	{
		ShoppingCart oldCart=getCart(cart.getId());
		
		if(oldCart==null)
		{
			return false;
		}
		
		String sql="Delete from ShoppingCart where id="+cart.getId();
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
	
	public boolean deleteAll(long userId)
	{
		String sql="delete from shoppingcart where userid="+userId;
		
		try
		{
			int row = db.execute(sql);
			
			return row>0?true:false;
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}finally
		{
			db.Close();
		}
		return false;
	}
}
